﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 后整理管理系统.自定义类;
using 纺织贸易管理系统.设置窗体;

namespace 纺织贸易管理系统.选择窗体
{
    public partial class 库存选择 : Sunny.UI.UIForm
    {
        public string StockName { get; set; } = "";
        public string CustomerName { get; set; } = string.Empty;
        public string Danhao { get; set; } = string.Empty;
        public List <StockTable > pingzhong { get; set; }
        private List<StockTable > dblist = new List<StockTable >();
        public 库存选择()
        {
            InitializeComponent();
            CreateGrid.Create(this.Name , gridView1);
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1); 
            dateTimePicker1.Value = DateTime.Now.AddDays(-180);
            //CreateGrid.Query<StockTable>(gridControl1, dblist.OrderByDescending(x=>x.ID ).ToList ());

            gridView1.OptionsCustomization.AllowSort = true;
        }
        public 库存选择(string ganghao,string yanse,string pingming)
        {
            InitializeComponent();
            CreateGrid.Create(this.Name, gridView1);
            dateTimePicker2.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            dateTimePicker1.Value = DateTime.Now.AddDays(-180);
            //CreateGrid.Query<StockTable>(gridControl1, dblist.OrderByDescending(x=>x.ID ).ToList ());

            gridView1.OptionsCustomization.AllowSort = true;
            txtganghao.Text = ganghao;
            txtpingming.Text = pingming;
            txtyanse.Text = yanse;
            Query();
        }

        private void 品种选择_Load(object sender, EventArgs e)
        {
            txtkehu.Text = CustomerName;
            Query();
        }
        private void Query()
        {
            if (string.IsNullOrEmpty(txtkehu.Text))
            {
                dblist = Connect.dbcontext.Queryable<StockTable>().Where(x => x.YS .Contains(txtyanse.Text) && x.PM.Contains(txtpingming.Text) && x.GG.Contains(txtguige.Text) && x.GH.Contains(txtganghao.Text) && x.kuanhao.Contains(txthuohao.Text) && x.CKMC.Contains(StockName)
           && x.CustomName.Contains(txtkehu.Text) && x.orderNum.Contains(txtOrderNum.Text) && x.RQ >= dateTimePicker1.Value.Date && x.RQ <= dateTimePicker2.Value && x.RKDH.Contains(Danhao)).OrderByDescending(x => x.ID).ToList();
            }
            else
            {
                if (checkBox1.Checked == false)
                {
                    if (checkBox2.Checked == false)
                    {
                        dblist = Connect.dbcontext.Queryable<StockTable>().Where(x => x.YS.Contains(txtyanse.Text) && x.PM.Contains(txtpingming.Text) && x.GG.Contains(txtguige.Text) && x.GH.Contains(txtganghao.Text) && x.kuanhao.Contains(txthuohao.Text) && x.CKMC.Contains(StockName)
                      && x.CustomName == txtkehu.Text && x.orderNum.Contains(txtOrderNum.Text) && x.RQ >= dateTimePicker1.Value.Date && x.RQ <= dateTimePicker2.Value && x.RKDH.Contains(Danhao)).OrderByDescending(x => x.ID).ToList();
                    }
                    else
                    {
                        dblist = Connect.dbcontext.Queryable<StockTable>().Where(x => x.IsCheckDone != "" && x.YS.Contains(txtyanse.Text) && x.PM.Contains(txtpingming.Text) && x.GG.Contains(txtguige.Text) && x.GH.Contains(txtganghao.Text) && x.kuanhao.Contains(txthuohao.Text) && x.CKMC.Contains(StockName)
                      && x.CustomName == txtkehu.Text && x.orderNum.Contains(txtOrderNum.Text) && x.RQ >= dateTimePicker1.Value.Date && x.RQ <= dateTimePicker2.Value && x.RKDH.Contains(Danhao)).OrderByDescending(x => x.ID).ToList();
                    }
                }
                else
                {
                    if (checkBox2.Checked == false)
                    {
                        dblist = Connect.dbcontext.Queryable<StockTable>().Where(x => x.YS.Contains(txtyanse.Text) && x.PM.Contains(txtpingming.Text) && x.GG.Contains(txtguige.Text) && x.GH.Contains(txtganghao.Text) && x.kuanhao.Contains(txthuohao.Text) && x.CKMC.Contains(StockName)
                    && x.CustomName == txtkehu.Text && x.orderNum.Contains(txtOrderNum.Text) && x.RQ >= dateTimePicker1.Value.Date && x.RQ <= dateTimePicker2.Value && x.LiuzhuanCard == string.Empty && x.RKDH.Contains(Danhao)).OrderByDescending(x => x.ID).ToList();
                    }
                    else
                    {
                        dblist = Connect.dbcontext.Queryable<StockTable>().Where(x => x.IsCheckDone != "" && x.YS.Contains(txtyanse.Text) && x.PM.Contains(txtpingming.Text) && x.GG.Contains(txtguige.Text) && x.GH.Contains(txtganghao.Text) && x.kuanhao.Contains(txthuohao.Text) && x.CKMC.Contains(StockName)
                     && x.CustomName == txtkehu.Text && x.orderNum.Contains(txtOrderNum.Text) && x.RQ >= dateTimePicker1.Value.Date && x.RQ <= dateTimePicker2.Value && x.LiuzhuanCard == string.Empty && x.RKDH.Contains(Danhao)).OrderByDescending(x => x.ID).ToList();
                    }
                }
            }
            gridControl1.DataSource = dblist;
        }
        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new StockTable() };
            fm.ShowDialog();
        }

        private void 品种选择_FormClosed(object sender, FormClosedEventArgs e)
        {       
            gridView1.CloseEditor();
            pingzhong = SelectHelper.GetSelectList<StockTable>(gridView1);
           
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            gridView1.SelectRow(gridView1.FocusedRowHandle);
            this.Close();
        }

        private void txtbianhao_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter )
            {
                Query();
            }
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Query();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            Query();
        }
    }
}
